<?php
class retrieve_comments_admin extends retrieve_base{

	private $module_display_options 	= array();

	/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();

	
		/**
		* Table structure for table 'page_comments'
		* 
		* the comments are grouped by the language of the translation and linked to the page so 
		* that the system will display all comments for the english version and not comments for all 
		* languages
		*/
		$fields = array(
			array("comment_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment","key"),
			array("comment_translation"		,"unsigned integer"			,"NOT NULL"	,"default '0'","key"),
			array("comment_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("comment_message"			,"text"						,"NOT NULL"	,"default ''"),
			array("comment_date"			,"datetime"					,"NOT NULL"	,"default ''"),
			array("comment_user"			,"unsigned integer"			,"NOT NULL"	,"default '0'","key"),
			array("comment_type"			,"unsigned small integer"	,"NOT NULL"	,"default '1'"),
			array("comment_response_to"		,"integer"					,"NOT NULL"	,"default '-1'","key"),
			array("comment_title"			,"varchar(255)"				,"NOT NULL"	,"default '-1'"),
			array("comment_status"			,"unsigned small integer"	,"NOT NULL"	,"default '1'"),
			array("comment_approved_by"		,"unsigned integer"			,"NOT NULL"	,"default '1'")
		);
		
		$primary ="comment_identifier";
		$tables[count($tables)] = array("page_comments", $fields, $primary);
		
		return $tables;
	}
	/**
	* This function is used to return the list of orders to be dealt with
	*/
	function getDigitalDesktop($parameters){
		$debug = $this->debugit(false, $parameters);
		$grp_info = $this->check_parameters($_SESSION,"SESSION_GROUP",Array());
		$max_grps = count($grp_info);
		$user   = $_SESSION["SESSION_USER_IDENTIFIER"];
		$access_list = "";
		$access_array = array();
		$ALL=0;
		$out ="";
		$open = 0;
		$userRestricted = 0;
		for($i=0;$i < $max_grps; $i++){
			$access = $grp_info[$i]["ACCESS"];
			$max = count($access);
			for($index=0;$index<$max;$index++){
				if ((substr($access[$index],0,strlen($this->module_command))==$this->module_command)||($access[$index]=="ALL")){
					$open=1;
				}
			}
		}
		$session_management_access	= $this->check_parameters($_SESSION, "SESSION_MANAGEMENT_ACCESS", Array());
		$session_man_access="";
		for($index=0,$max=count($session_management_access);$index<$max;$index++){
			$userRestricted=1;
			if ($index>0){
				$session_man_access .= ",";
			}
			$session_man_access .= " ".$session_management_access[$index];
		}
		$join1 ="";
		$join2 ="";
		$where = "";
		if ($userRestricted){
			$join1  = " inner join menu_access_to_page on (page_trans_data.trans_identifier = menu_access_to_page.trans_identifier and page_trans_data.trans_client = menu_access_to_page.client_identifier) ";
			$join1 .= " left outer join relate_user_menu on (relate_user_menu.menu_identifier = menu_access_to_page.menu_identifier and relate_user_menu.user_identifier = page_trans_data.trans_doc_lock_to_user) ";
			$join2  = " inner join menu_access_to_page on (page_trans_data.trans_identifier = menu_access_to_page.trans_identifier and page_trans_data.trans_client = menu_access_to_page.client_identifier) ";
			$join2 .= " left outer join relate_user_menu on (relate_user_menu.menu_identifier = menu_access_to_page.menu_identifier) ";
			$where = " and relate_user_menu.menu_identifier in ($session_man_access)";
			$link_user = $user;
		} else {
			$link_user = -1;
		}
		if ($open==1){
			if (($this->parent->server[LICENCE_TYPE]==ECMS)||($this->parent->server[LICENCE_TYPE]==MECM)){
//					$out="<cmd label=\"".LOCALE_LIST."\">COMMENTSADMIN_LIST</cmd>";
					$sql    = "select trans_title, trans_page, count(comment_translation) as total from page_trans_data 
								inner join page_comments on page_trans_data.trans_identifier = comment_translation 
								$join2
								where comment_client=$this->client_identifier and comment_status=1 and comment_type='0'
								group by page_trans_data.trans_page, page_trans_data.trans_title
								$where
							";
					if ($debug) print "<p><strong>:: ".__FILE__." @ ".__LINE__." ::</strong><br/>$sql</p>";
					$result = $this->db->query($sql);
					if ($this->db->num_rows($result)>0){
						while ($r=$this->db->fetch_array($result)){
							$out .= "<text><![CDATA[";
							$out .= "<a href='admin/index.php?command=COMMENTSADMIN_VIEW_LIST&amp;lock=1&amp;identifier=".$r["trans_page"]."'>";
							$out .= $this->split_me($this->split_me($r["trans_title"],"&#39;","'"),"&quot;",'"')." (".$r["total"].")";
							$out .= "</a>";
							$out .= "]]></text>";
						}
					}else{
						$out .= "<text><![CDATA[".LOCALE_SORRY_NO_WEB_COMMENTS."]]></text>";
					}
			return "<module name=\"$this->module_name\" label=\"".MANAGEMENT_COMMENTS."\" display=\"my_workspace\">".$out."</module>";
			}
		}else{
			return "";
		}
	}
}
?>